package com.hey.qrcode.common.utils;

import java.util.Map;


/**
 * Created by hey on 2018/1/15.
 */
public class WxUtil {
    private static final String APP_SECRET = "6091c400d2280c63542555f8342e5232";
    private static final String WX_APP_ID = "wxa2b07e9868bcef43";
    private static final String GRANT_TYPE = "client_credential";
    private static final String TICKET_CODE_URL = "https://api.weixin.qq.com/cgi-bin/qrcode/create";
    private static final String QR_CODE_URL = "https://mp.weixin.qq.com/cgi-bin/showqrcode";
    private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";
    private static final String USER_INFO_URL = "https://api.weixin.qq.com/cgi-bin/user/info";
    private static final String LANG = "zh_CN";

    //获取ACCESS_TOKEN
    public String getWxAccessToken(){
        String result =  HttpRequestUtil.sendGet(TOKEN_URL,"grant_type="+GRANT_TYPE+"&appid="+WX_APP_ID+"&secret="+APP_SECRET);
        Map map = String2Map.getValueGson(result);
        String token = map.get("access_token").toString();
        return token;
    }

    //获取二维码
    public String getQrCodeUrl(){
        String url = TICKET_CODE_URL+"?access_token="+getWxAccessToken();
        String postPara = "{'action_name': 'QR_LIMIT_STR_SCENE', 'action_info': {'scene': {'scene_str': 'test'}}}";
        String result =  HttpRequestUtil.httpsRequest(url,"POST",postPara);
        Map map = String2Map.getValueGson(result);
        String ticket = map.get("ticket").toString();
        String codeUrl = map.get("url").toString();
//        HttpRequestUtil.sendGet(QR_CODE_URL+"")
        System.out.println("ticket:"+ticket);
        System.out.println("codeUrl:"+codeUrl);
        return ticket;
    }


    //获取用户信息
    public static Map getUserInfo(String accessToken,String openid){
        String result = HttpRequestUtil.sendGet(USER_INFO_URL,"access_token="+accessToken+"&openid="+openid+"&lang="+LANG);
        Map infoMap = String2Map.getValueGson(result);
        return infoMap;
    }

}
